﻿using System;
using System.Windows.Forms;
using SurveySystem;
using SurveySystem.Repository;
using SurveySystem.Repository.MySQL;
using SurveySystem.GUI.Forms;
using xn;
using SurveySystem.Factory;
using SurveySystem.Model;
using SurveySystem.Admin.Screens;
using SurveySystem.Controllers;
using KinectCore.Gui.Controller;
using System.Drawing;

namespace Testing
{
    static class Program
    {

        static MouseEmulatorController kinectController;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptions);
            SurveySystem.Utility.Globals.ConnectionString = "Server=paddyvs.mine.nu;Database=macsboards;Uid=macsuser;Pwd=macsuser;";

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            ISurveyRepository surveyRepository = new MySQLSurveyRepository();
            ISurveyQuestionRepository questionRepository = new MySQLSurveyQuestionRepository();
            ISurveyAnswerRepository answerRepository = new MySQLAnswerRepository();
            ISurveyResponseRepository responseRepository = new MySQLResponseRepository();
            ISurveyResponseAnswerRepository responseAnswerRepository = new MySQLResponseAnswerRepository();
#if DEBUG

            Context ctx = null;
            // PSK - Added this so we could test without kinects. Most UI doesn't exclusively rely on Kinect anymore
            try
            {
                ctx = new Context("Config/KinectConfig.xml");
            }
            catch (Exception ex)
            {
                // nowt we can do
                Console.Error.WriteLine(ex.Message);
            }

            SurveyMainForm mainForm = new SurveyMainForm();

            kinectController = new MouseEmulatorController(ctx);
            //kinectController.HandTracking += new HandTrackingStartEvent(controller_HandTracking);
            //kinectController.HandNotTracking += new HandTrackingStopEvent(controller_HandNotTracking);
            kinectController.ParentControl = mainForm;

            SurveyFactory fact = new SurveyFactory(surveyRepository, questionRepository, answerRepository);
            ResultFactory fact2 = new ResultFactory(responseRepository, responseAnswerRepository);
            Survey s = surveyRepository.GetAllActiveSurveys()[0];
            fact.PopulateSurvey(s);

            //SurveyController cont = new SurveyController(new SurveyMainForm(), ctx, s, fact2);
            //cont.ShowSurveyWelcome();

            SureveySelectionController selectionController = new SureveySelectionController(mainForm, fact, fact2, surveyRepository);
            selectionController.Close += new CLoseEvent(selectionController_Close);
            selectionController.ShowSelectionScreen();

            Application.Run(selectionController.MainForm);

#else

            // create a new instance of the survey list form
            SurveyListForm frm = new SurveyListForm();

            // assign repositories
            frm.SurveyRepository = surveyRepository;
            frm.QuestionRepository = questionRepository;
            frm.AnswerRepository = answerRepository;
            frm.ResponseRepository = responseRepository;
            frm.ResponseAnswerRepository = responseAnswerRepository;


            try
            {
                Application.Run(frm);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
#endif

        }

        static void selectionController_Close()
        {
            kinectController.Stop();
            Environment.Exit(0);
        }

        static void controller_HandNotTracking(Control c)
        {
            c.BackColor = Color.Blue;
        }

        static void controller_HandTracking(Control c)
        {
            c.BackColor = Color.Green;
        }


        static void UnhandledExceptions(object sender, UnhandledExceptionEventArgs e)
        {
            string source = "SOTest";
            if (!System.Diagnostics.EventLog.SourceExists(source))
            {
                System.Diagnostics.EventLog.CreateEventSource(source, "Application");
            }

            System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
            log.Source = source;

            log.WriteEntry(e.ExceptionObject.ToString(), System.Diagnostics.EventLogEntryType.Error);

            Console.Error.WriteLine(Environment.StackTrace.ToString());


        }
    }
}
